﻿#region
/**
 * $Date: 0001-1-1
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;
#endregion

namespace BuXiaChu.OrderDishes.DAL
{
    public class EmployeesDAL
    {
        #region private const
        private const string PARAM_ID = "@Id";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_GENDER = "@Gender";
        private const string PARAM_CITY_ID = "@CityId";
        private const string PARAM_AREA_ID = "@AreaId";
        private const string PARAM_SERVICE_STATION_ID = "@ServiceStationId";
        private const string PARAM_MOBILE_TELEPHONE = "@MobileTelephone";
        private const string PARAM_STATUS = "@Status";
        private const string PARAM_JOB_TITLE = "@JobTitle";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_UPDATE_DATE = "@UpdateDate";
        private const string PARAM_COMMENT = "@Comment";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, EmployeesModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.Id = reader.IsDBNull(reader.GetOrdinal("Id")) ? 0 : reader.GetInt32(reader.GetOrdinal("Id"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.Gender = reader.IsDBNull(reader.GetOrdinal("Gender")) ? 0 : reader.GetInt32(reader.GetOrdinal("Gender"));
                obj.CityId = reader.IsDBNull(reader.GetOrdinal("CityId")) ? 0 : reader.GetInt32(reader.GetOrdinal("CityId"));
                obj.AreaId = reader.IsDBNull(reader.GetOrdinal("AreaId")) ? 0 : reader.GetInt32(reader.GetOrdinal("AreaId"));
                obj.ServiceStationId = reader.IsDBNull(reader.GetOrdinal("ServiceStationId")) ? 0 : reader.GetInt32(reader.GetOrdinal("ServiceStationId"));
                obj.MobileTelephone = reader.IsDBNull(reader.GetOrdinal("MobileTelephone")) ? String.Empty : reader.GetString(reader.GetOrdinal("MobileTelephone"));
                obj.Status = reader.IsDBNull(reader.GetOrdinal("Status")) ? Convert.ToByte(0) : reader.GetByte(reader.GetOrdinal("Status"));
                obj.JobTitle = reader.IsDBNull(reader.GetOrdinal("JobTitle")) ? 0 : reader.GetInt32(reader.GetOrdinal("JobTitle"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
                obj.UpdateDate = reader.IsDBNull(reader.GetOrdinal("UpdateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("UpdateDate"));
                obj.Comment = reader.IsDBNull(reader.GetOrdinal("Comment")) ? String.Empty : reader.GetString(reader.GetOrdinal("Comment"));
            }
            #endregion
        }

        private static SqlParameter[] GetEmployeesParams(EmployeesModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_ID, SqlDbType.Int,0,obj.Id),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 20,obj.Name),					
					 SQLHelper.MakeParam(PARAM_GENDER, SqlDbType.Int,0,obj.Gender),
					 SQLHelper.MakeParam(PARAM_CITY_ID, SqlDbType.Int,0,obj.CityId),
					 SQLHelper.MakeParam(PARAM_AREA_ID, SqlDbType.Int,0,obj.AreaId),
					 SQLHelper.MakeParam(PARAM_SERVICE_STATION_ID, SqlDbType.Int,0,obj.ServiceStationId),
					 SQLHelper.MakeParam(PARAM_MOBILE_TELEPHONE, SqlDbType.VarChar, 15,obj.MobileTelephone),					
					 SQLHelper.MakeParam(PARAM_STATUS, SqlDbType.TinyInt,0,obj.Status),
					 SQLHelper.MakeParam(PARAM_JOB_TITLE, SqlDbType.Int,0,obj.JobTitle),
					 SQLHelper.MakeParam(PARAM_COMMENT, SqlDbType.NVarChar, 256,obj.Comment)					
			};

            return dbParams;
            #endregion
        }

        public static bool Save(EmployeesModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetEmployeesParams(obj);

                if (obj.Id == 0)
                {
                    obj.Id = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Employees_InsertUpdate", dbParams));
                    return obj.Id > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Employees_InsertUpdate", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Employees-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }

        public static EmployeesModel GetEmployeesById(int id)
        {
            #region
            SqlDataReader reader = null;
            EmployeesModel obj = new EmployeesModel();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_ID, SqlDbType.Int, 0, id)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Employees_GetByEmployeesId", dbParams);

                if (reader.Read())
                {
                    FillModelFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Employees-->GetSingleObject-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static bool Delete(int id)
        {
            #region
            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_ID, SqlDbType.Int, 0, id)
                    };

                return SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Employees_Delete", dbParams) > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Employees-->Delete-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<EmployeesModel> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            #region
            List<EmployeesModel> result = new List<EmployeesModel>();
            pageCount = 0;
            try
            {
                if (string.IsNullOrEmpty(sqlWhere))
                {
                    sqlWhere = " 1=1 ";
                }

                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam("@sqlWhere", SqlDbType.NVarChar,0,sqlWhere),
                    SQLHelper.MakeParam("@pageIndex",SqlDbType.Int,0,pageIndex),
                    SQLHelper.MakeParam("@pageSize",SqlDbType.Int,0,pageSize),
                    SQLHelper.MakeOutParam("@pageCount", SqlDbType.Int,0,0)                    
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Employees_Search", dbParams))
                {
                    while (reader.Read())
                    {
                        EmployeesModel model = new EmployeesModel();
                        FillModelFrom(reader, model);
                        result.Add(model);
                    }
                }
                pageCount = int.Parse(dbParams[3].Value.ToString());
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Employees-->Search-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }
            return result;
            #endregion
        }

        public static List<EmployeesModel> GetEmployeesByJobTitle(int jobTitle)
        {
            #region
            List<EmployeesModel> result = new List<EmployeesModel>();
            try
            {
                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam(PARAM_JOB_TITLE, SqlDbType.Int,0,jobTitle)            
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Employees_GetByJobTitle", dbParams))
                {
                    while (reader.Read())
                    {
                        EmployeesModel model = new EmployeesModel();
                        FillModelFrom(reader, model);
                        result.Add(model);
                    }
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Employees-->GetEmployeesByJobTitle-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }
            return result;
            #endregion
        }
    }
}
